package com.cne.gmail.Gmail.Handle;

import com.cne.gmail.Gmail.model.*;
import com.google.api.services.gmail.model.MessagePart;
import com.google.api.services.gmail.model.MessagePartBody;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 读收件箱内容
 * 1.如果本地数据库没有messageId的内容，则拉取gmail里的内容存储到momgoDB中，
 * mongoDB存储文本，上传到阿里云服务器的URL，以及附件的URL
 */
public class PartsHandle {
    private List<AttachMentIdModel> attachMentIdModels=Lists.newArrayList();
    /**
     * 递归，求所有图片以及附件的attchmentId,文件名，媒体类型
     * @param parts
     * @return
     */
    public List recurrenceGetData(List<MessagePart> parts){
        if (parts.size()!=0&&parts!=null){
            for (MessagePart part:parts){
                MessagePartBody body= part.getBody();
                if (body.size()!=1){
                    AttachMentIdModel attachMentIdModel=new AttachMentIdModel();
                    attachMentIdModel.setAttchMentId(body.getAttachmentId());
                    attachMentIdModel.setData(body.getData());
                    attachMentIdModel.setMedieType(part.getMimeType());
                    attachMentIdModel.setFileName(part.getFilename());
                    attachMentIdModels.add(attachMentIdModel);
                }else {
                 parts=  part.getParts();
                 recurrenceGetData(parts);
                }
            }
        }
        return attachMentIdModels;
    }
    public static MessageModel buildMongoData(String messageId, MailSenderInfo mail) {
        MessageModel messageModel = new MessageModel();
        if (messageId != null) {
            messageModel.setMessageId(messageId);
            messageModel.setSubject(mail.getSubject());
            messageModel.setFrom(mail.getFrom());
            messageModel.setHtml(mail.getContent());
            messageModel.setPlain(mail.getContent());
            if (mail.getCc().size() != 0) {
                messageModel.setCc(StringUtils.join(mail.getCc(), ","));
            }
            if (mail.getReceivers().size() != 0) {
                messageModel.setTo(StringUtils.join(mail.getReceivers(), ","));
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long lt = new Long(System.currentTimeMillis());
            Date date = new Date(lt);
            messageModel.setIntnetdate(System.currentTimeMillis());
            messageModel.setDate(simpleDateFormat.format(date));
            //处理附件和图片向messageModel里添加数据
            List<AttchmentModel> attchmentModels = Lists.newArrayList();
            if (mail.getAttchmentModels()!=null&mail.getAttchmentModels().size() != 0) {
                for (AttchmentModel attchmentModel : mail.getAttchmentModels()) {
                    attchmentModels.add(attchmentModel);
                }
            }
            if (mail.getContextImgModels()!=null&mail.getContextImgModels().size() != 0) {
                for (ContextImgModel contextImgModel : mail.getContextImgModels()) {
                    AttchmentModel attchmentModel = new AttchmentModel();
                    attchmentModel.setFileName(contextImgModel.getFileName());
                    attchmentModel.setUrl(contextImgModel.getUrl());
                    attchmentModels.add(attchmentModel);
                }
            }
            messageModel.setAttachMentModels(attchmentModels);
            messageModel.setLabel("SENT");
            messageModel.setStatus(0);
        }
        return messageModel;
    }

}
